package graphicinterface;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JPanel;



public class JPanelWithBackgroundImage extends JPanel 
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String filenameBackgroundImage;
	private int panelWidth;
	private int panelHeight;
	
	public JPanelWithBackgroundImage(String filenameBackgroundImage)
	{
		this.filenameBackgroundImage = filenameBackgroundImage;
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		this.setPreferredSize(d);
		panelWidth = (int) d.getWidth() - 15;
		panelHeight = (int) d.getHeight() - 20;
	}
	public void paintComponent(Graphics g) 
	{          
		   super.paintComponent(g);       
		   ImageIcon icon = CreatesImageIcon.createImageIcon("images/" + filenameBackgroundImage, "default background image");       
		   Image imagem = icon.getImage();
		   Image imagemRedimensionada = this.redimensionImage(imagem, panelWidth , panelHeight);

		   g.drawImage(imagemRedimensionada, 0, 0, this);  
	}  
	
	private BufferedImage getBufferedImageFromImage(Image img)
    {
		//extracted from site http://www.javareference.com/jrexamples/viewexample.jsp?id=112
        //This line is important, this makes sure that the image is
        //loaded fully
        img = new ImageIcon(img).getImage();
        
        //Create the BufferedImage object with the width and height of the Image
        BufferedImage bufferedImage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);

        //Create the graphics object from the BufferedImage
        Graphics g = bufferedImage.createGraphics();

        //Draw the image on the graphics of the BufferedImage
        g.drawImage(img, 0, 0, null);
        
        //Dispose the Graphics
        g.dispose();
        
        //return the BufferedImage
        return bufferedImage;
    } 
	
	private static BufferedImage resize(BufferedImage image, int width, int height) 
	{
		//extracted from site http://www.componenthouse.com/article-20
		BufferedImage resizedImage = new BufferedImage(width, height,
		BufferedImage.TYPE_INT_ARGB);
		Graphics2D g = resizedImage.createGraphics();
		g.drawImage(image, 0, 0, width, height, null);
		g.dispose();
		return resizedImage;
	} 
	
	public static Image toImage(BufferedImage bufferedImage) 
	{
		//extracted from site http://www.exampledepot.com/egs/java.awt.image/Buf2Image.html
	    return Toolkit.getDefaultToolkit().createImage(bufferedImage.getSource());
	}
	
	private Image redimensionImage(Image image, int width, int height)
	{
		BufferedImage buff = getBufferedImageFromImage(image);
		BufferedImage buffResized = resize(buff, width, height);
		Image imageResized = toImage(buffResized);
		return imageResized;
	}

}
